package com.iflytek.uoamp.agent.monitor.find.imp.baseitem;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.iflytek.uoamp.agent.common.util.ReadFileByLine;
import com.iflytek.uoamp.agent.core.sender.monitorsender.responseObj.Program;
import com.iflytek.uoamp.agent.monitor.find.Finder;

public abstract class FindBaseMonitorItemAbs implements Finder {
	private String path = null;
	private static Set<String> set = new HashSet<String>();
	
	/***
	 * 获取磁盘信息
	 * @return
	 */
	abstract protected List<Program> getDisks();
	
	
	/***
	 * 添加CPU监控项
	 * @return
	 */
	abstract protected List<Program> getCPU();
	
	/***
	 * 添加内存监控项
	 * @return
	 */
	abstract protected Program getMemory();
	
	/***
	 * 获取ip地址，添加被监控主机
	 * @return
	 */
	abstract protected Program getInterface() ;

	/***
	 * 获取磁盘io监控
	 * @return
	 */
	abstract protected List<Program> getDiskIO();

    /***
     * 添加CPU详细信息
     * @return
     */
    abstract protected Program getCPUInfo();

    /***
     * 添加内存详细信息
     * @return
     */
    abstract protected Program getMemoryInfo();

    /***
     * 添加操作系统信息
     * @return
     */
    abstract protected Program getOsInfo();
    
    /***
     * 添加JDK信息
     * @return
     */
    abstract protected Program getJdkVersion();
    
    /***
     * 获取系统运行时间
     * @return
     */
    abstract protected Program getRunTime();

	/**
	 * 初始化及其发现类;
	 * @param path;是允许的 IP 头的配置文件;
	 */
	public FindBaseMonitorItemAbs(String path){
		this.path = path;
	}
	
	
	
	public static Set<String> getSet() {
		return set;
	}

	public static void setSet(HashSet<String> set) {
		FindBaseMonitorItemAbs.set = set;
	}

	/**
	 * 初始化允许的 IP头
	 */
	public void init(){
		Logger.getRootLogger().info("init FindBaseMonitorItem and path = "+path);
		List<String> line = ReadFileByLine.getAllLine2Array(path, "utf-8", 0, -1);
		for(String cur:line){
			Logger.getRootLogger().info("add allow head "+cur);
			set.add(cur);
		}
	}
	
	/**
	 * 重新初始化允许的 IP头;
	 * 从 Server 处更新 agent 的时候会触发调用
	 */
	public void reSet(){
		set.clear();
		init();
	}
	
	
	
	@Override
	public List<Program> getMonitorItems() {
		List<Program> list = new LinkedList<Program>();
		List<Program> programs = getDisks();
		if(programs!=null){
			list.addAll(programs);
		}
		
		programs = getDiskIO();
		if(programs!=null){
			list.addAll(programs);
		}
				
		programs = getCPU();
		if(programs!=null){
			list.addAll(programs);
		}
		
		list.add(getMemory());
		
		Program program = getInterface();
		if(program!=null){
			list.add(program);
		}

        list.add(getCPUInfo());

        list.add(getMemoryInfo());

        list.add(getOsInfo());
        
        list.add(getJdkVersion());
        
        list.add(getRunTime());
		
		return list;
	}

}
